package contacts.view;

import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import contacts.controller.ContactsControllerListener;
import contacts.po.Student;

public class ContactsDialog extends JDialog {
	private static final long serialVersionUID = 1L;
	private ContactsControllerListener contactsControllerListener;
	private JButton confirmButton;
	private JPanel studentAddPanel;
	private Student student;
	private JTextField studentIdTextField;
	private JTextField studentNoTextField;
	private JTextField roomNoTextField;
	private JTextField studentNameTextField;
	private JTextField telephoneTextField;
	private JTextField emailTextField;
	private JTextField birthPlaceTextField;
	private JTextField birthdayTextField;
	private JTextField addressTextField;
	private JTextField roomTextField;
	private JTextField roomNameTextField;

	public ContactsDialog(ContactsFrame owner, String title, Student student) {
		this.contactsControllerListener = owner.getControllerListener(); // 让contactsFrame和contactsAddDialog共用同一个controller对象
		this.contactsControllerListener.setStudentAddDialog(this);
		this.setTitle(title);
		this.student = student;
		this.add(getStudentAddPanel());
		this.setModal(true); // 设有模
		this.setSize(370, 530);
		this.setLocationRelativeTo(null);
		this.setResizable(false);
		this.setVisible(true);
		this.validate();
	}

	public void closeCurrentDialog() {
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);

	}

	/**
	 * 添加/修改联系人表单面板
	 * 
	 * @return
	 */
	private JPanel getStudentAddPanel() {
		studentAddPanel = new JPanel();
		studentAddPanel.setBackground(Color.orange);
		if ("添加联系人".equals(this.getTitle())) {
			studentIdTextField = new JTextField(30);
			studentNoTextField = new JTextField(30);
			studentNameTextField = new JTextField(30);
			roomNoTextField = new JTextField(30);
			// birthdayTextField = new JTextField(30);
			telephoneTextField = new JTextField(30);
			emailTextField = new JTextField(30);
			birthPlaceTextField = new JTextField(30);
			addressTextField = new JTextField(30);
			confirmButton = new JButton("确定添加");
			confirmButton.setActionCommand("addPost"); // 添加联系人
		} else {
			studentIdTextField = new JTextField(String.valueOf(student.getStudentId()), 30);
			studentNoTextField = new JTextField(String.valueOf(student.getStudentNo()), 30);
			studentNameTextField = new JTextField(student.getStudentName(), 30);
			roomNoTextField = new JTextField(String.valueOf(student.getRoom().getRoomNo()), 30);
			// birthdayTextField= new JTextField(String.valueOf(student.getBirthday()),30);
			telephoneTextField = new JTextField(student.getTelephone(), 30);
			emailTextField = new JTextField(student.getEmail(), 30);
			birthPlaceTextField = new JTextField(student.getBirthPlace(), 30);
			addressTextField = new JTextField(student.getAddress(), 30);
			confirmButton = new JButton("确定修改");
			confirmButton.setActionCommand("updatePost"); // 修改联系人
		}
		confirmButton.addActionListener(contactsControllerListener);
		studentAddPanel.add(new JLabel("编号"));
		studentAddPanel.add(studentIdTextField);
		studentAddPanel.add(new JLabel("学号"));
		studentAddPanel.add(studentNoTextField);
		studentAddPanel.add(new JLabel("姓名"));
		studentAddPanel.add(studentNameTextField);
		// studentAddPanel.add(new JLabel("生日"));
		// studentAddPanel.add(birthdayTextField);
		studentAddPanel.add(new JLabel("电话"));
		studentAddPanel.add(telephoneTextField);
		studentAddPanel.add(new JLabel("电子邮件"));
		studentAddPanel.add(emailTextField);
		studentAddPanel.add(new JLabel("籍贯"));
		studentAddPanel.add(birthPlaceTextField);
		studentAddPanel.add(new JLabel("家庭地址"));
		studentAddPanel.add(addressTextField);
		studentAddPanel.add(new JLabel("宿舍号"));
		studentAddPanel.add(roomNoTextField);
		studentAddPanel.add(confirmButton);
		return studentAddPanel;
	}

	public JTextField getStudentIdTextField() {
		return studentIdTextField;
	}

	public void setStudentIdTextField(JTextField studentIdTextField) {
		this.studentIdTextField = studentIdTextField;
	}

	public JTextField getStudentNoTextField() {
		return studentNoTextField;
	}

	public void setStudentNoTextField(JTextField studentNoTextField) {
		this.studentNoTextField = studentNoTextField;
	}

	public JTextField getRoomNoTextField() {
		return roomNoTextField;
	}

	public void setRoomNoTextField(JTextField roomNoTextField) {
		this.roomNoTextField = roomNoTextField;
	}

	public JTextField getStudentNameTextField() {
		return studentNameTextField;
	}

	public void setStudentNameTextField(JTextField studentNameTextField) {
		this.studentNameTextField = studentNameTextField;
	}

	public JTextField getTelephoneTextField() {
		return telephoneTextField;
	}

	public void setTelephoneTextField(JTextField telephoneTextField) {
		this.telephoneTextField = telephoneTextField;
	}

	public JTextField getEmailTextField() {
		return emailTextField;
	}

	public void setEmailTextField(JTextField emailTextField) {
		this.emailTextField = emailTextField;
	}

	public JTextField getBirthPlaceTextField() {
		return birthPlaceTextField;
	}

	public void setBirthPlaceTextField(JTextField birthPlaceTextField) {
		this.birthPlaceTextField = birthPlaceTextField;
	}

	public JTextField getBirthdayTextField() {
		return birthdayTextField;
	}

	public void setBirthdayTextField(JTextField birthdayTextField) {
		this.birthdayTextField = birthdayTextField;
	}

	public JTextField getAddressTextField() {
		return addressTextField;
	}

	public void setAddressTextField(JTextField addressTextField) {
		this.addressTextField = addressTextField;
	}

	public JTextField getRoomTextField() {
		return roomTextField;
	}

	public void setRoomTextField(JTextField roomTextField) {
		this.roomTextField = roomTextField;
	}

	public JTextField getRoomNameTextField() {
		return roomNameTextField;
	}

	public void setRoomNameTextField(JTextField roomNameTextField) {
		this.roomNameTextField = roomNameTextField;
	}

	public JButton getConfirmButton() {
		return confirmButton;
	}

}
